<?php


namespace app\app\controller;


use app\app\model\AttrModel;
use app\app\model\AttrValModel;
use app\app\model\GoodsModel;
use app\app\validate\AttrValidate;
use app\app\validate\GoodsValidate;
use cmf\controller\AdminBaseController;

class GoodsController extends AdminBaseController
{
    //商品首页
    public function index()
    {
        $name = input('name','');
        $where = [];
        if (!empty($name)) {
            $where[] = ['name','like' ,"%$name%"];
        }
        $goods = GoodsModel::order('create_time','DESC')
            ->where($where)
            ->paginate(5);
        $goods->appends(request()->param());
        return $this->fetch('index',[
            'Goods' => $goods
        ]);
    }
    //添加页面
    public function add()
    {
        return $this->fetch();
    }
    //添加功能
    public function addPost()
    {

        $data = input('post.');
        $validate = new GoodsValidate();
        //验证器
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $data['images'] = implode(',',$data['images']);
        $data['create_time'] = date('Y-m-d H:i:s');
        $banner = GoodsModel::create($data);
        if ($banner) {
            $this->success('添加成功','Goods/index');
        }else{
            $this->error('添加失败');
        }
    }
    //修改页面
    public function edit()
    {

        $id = input('id');
        if (!$id){
            $this->error('错误');
        }
        $goods = GoodsModel::find($id);
        if (!$goods) {
            $this->error('错误');
        }
        return $this->fetch('edit',[
            'Goods' => $goods
        ]);
    }
    //修改功能
    public function editPost()
    {
        $data = input('post.');
        if (!$data['id']) {
            $this->error('ID错误');
        }
        $validate = new GoodsValidate();
        //验证器
        $res = $validate->scene('edit')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $Goods = GoodsModel::find($data['id']);
        if (!$Goods) {
            $this->error('错误');
        }
       // $Goods->images = $data['images'];
        $Goods->image = $data['image'];
        $Goods-> name = $data['name'];
        $Goods-> url = $data['url'];
        $Goods-> type = $data['type'];

        if ($Goods->save()) {
            $this->success('保存成功','Goods/index');
        }else{
            $this->error('保存失败');
        }
    }
    //删除功能
    public function del()
    {
        $id = input('id');
        if (!$id) {
            $this->error('错误');
        }
        $Goods = GoodsModel::find($id);
        if ($Goods->delete()) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //配置属性
    public function getAttrVal()
    {
        $id = input('id/d');
        return $this->fetch('get_attr_val',[
            'id' => $id
        ]);
    }
    //添加属性值
    public function getAttrValPost()
    {
        $id = input('gid/d');
        $from = input('form');
        if (!$id && $from) {
            $this->error('参数错误');
        }
        $goods = GoodsModel::find($id);
        if (!$goods) {
            $this->error('错误');
        }
        $goods->val_info = json_encode($from);
        if ($goods->save()) {
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }
    //配置属性
    public function addAttr()
    {
        $id = input('id');
        return $this->fetch('attr',[
            'id' =>$id
        ]);
    }
    //添加商品属性
    public function addAttrPost()
    {
        $data = input('form');
        if (!$data) {
            return json([
                'code' => 0,
                'msg' => '参数错误',
                'data' => null
            ]);
        }
        //halt($data);
        $attr =new AttrModel();
        if ($attr->saveAll($data)) {
            return json([
                'code' => 1,
                'msg' => '添加成功',
                'data' => null
            ]);

        }else{
            return json([
                'code' => 0,
                'msg' => '添加失败',
                'data' => null
            ]);
        }
    }
    //配置商品属性值
    public function addAttrVal()
    {
        $id = input('id');

        return $this->fetch('attr_val',[
            'id' =>$id
        ]);
    }
    //添加商品属性值
    public function addAttrValPost()
    {
        $data = input('form');
        // halt($data);
        if (!$data){
            return json([
                'code' => 0,
                'msg' => '错误',
            ]);
        }
        $insert_data = [];
        foreach ($data as $v) {
            $val = [
                'attr_id' => $v['attr'],
                'attr_val' => $v['attrValue']
            ];
            $insert_data[] = $val;
        }
        // halt($insert_data);
        $attr = new AttrValModel();
        if ($attr->saveAll($insert_data)) {
                return json([
                    'code' => 1,
                    'msg' => '添加成功',
                    'data' => null
            ]);

        }else{
            return json([
                'code' => 0,
                'msg' => '添加失败',
                'data' => null
            ]);
        }

    }
    //获取商品属性下拉框
    public function attrList()
    {
        $id = input('gid/d');
        if (empty($id)) {
            return json([
                'code' => 0,
                'msg' => '参数错误',
                'data' => null
            ]);
        }
        $data = AttrModel::where('gid',$id)->select();
        //halt($data);
        if (!$data) {
            return json([
                'code' => 0,
                'msg' => '错误',
                'data' => null
            ]);
        }
        return json([
            'code' => 1,
            'msg' => '成功',
            'data' => $data
        ]);

    }
}